﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KlauserswagModel;
using WebShopDAL;

namespace ProjektarbeteKlauserSwag
{
    public partial class CartPayForm : System.Web.UI.Page
    {
        List<OrderRow> cart;
        List<CartArticle> cartArticles;
        User currentUser;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //cart = Session["Order"] as List<OrderRow>;
                if (Session["Order"] != null)
                {
                    cart = Session["Order"] as List<OrderRow>;
                    cartArticles = new List<CartArticle>();

                    foreach (OrderRow article in cart)
                    {
                        cartArticles.Add(new CartArticle(article.ArticleID, article.Quantity));
                    }
                }
                else
                {
                    cartArticles = new List<CartArticle>();

                }

                if (Session["CurrentUser"] != null)
                {
                    currentUser = Session["CurrentUser"] as User;
                }
                if (!IsPostBack)
                {
                    FillUserInfo();
                    UpdateCart();
                }
            }
            catch (Exception)
            {
            }
        }

        private void FillUserInfo()
        {
            TextBoxFirstName.Text = currentUser.Firstname;
            TextBoxLastName.Text = currentUser.Lastname;
            TextBoxAddress.Text = currentUser.Address;
            TextBoxZip.Text = currentUser.Zip;
            TextBoxCity.Text = currentUser.City;
            TextBoxEmail.Text = currentUser.Email;
            TextBoxPhone.Text = currentUser.Phone;
        }

        private void ShowCart()
        {
            GridView1.DataSource = cartArticles;
            GridView1.DataBind();

            int total = 0;
            foreach (var article in cartArticles)
            {
                total += article.Quantity * (int)article.UnitPrice;
            }
            LabelTotal.Text = total.ToString() + " SEK";
        }

        protected void ButtonDeleteArticle_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if ((row.FindControl("Remove") as CheckBox).Checked)
                    {
                        int index = row.RowIndex;
                        cartArticles.RemoveAt(index);
                        cart.RemoveAt(index);
                    }
                }
                UpdateCart();
                Response.Redirect("~/CartPayForm.aspx");
            }
            catch (Exception)
            {
            }
        }

        protected void ButtonFillUserInfo_Click(object sender, EventArgs e)
        {
            try
            {
                FillUserInfo();
            }
            catch (Exception)
            {

            }
        }

        private void UpdateCart()
        {
            if (cartArticles.Count > 0)
            {
                LabelNothingInCart.Visible = false;
                ButtonPlaceOrder.Enabled = true;
                ButtonDeleteItem.Enabled = true;
                ShowCart();
            }
            else
            {
                LabelNothingInCart.Text = "Your cart is empty!";
                LabelNothingInCart.Visible = true;
                ButtonPlaceOrder.Enabled = false;
                ButtonDeleteItem.Enabled = false;
                ShowCart();
            }
        }

        protected void ButtonPlaceOrder_Click(object sender, EventArgs e)
        {
            try
            {
                PlaceOrder();
            }
            catch (Exception)
            {
            }
        }
        private void PlaceOrder()
        {
            if (cartArticles.Count > 0)
            {
                LabelNothingInCart.Visible = false;

                OrderHeadDB.CreateOrderHead(
                    new OrderHead{
                        UserID = currentUser.UserID,
                        Address = TextBoxAddress.Text,
                        Zip = TextBoxZip.Text,
                        City = TextBoxCity.Text,
                        Processed = "false"});

                OrderHead orderHead = OrderHeadDB.GetOrderHeadByUserID(currentUser.UserID);

                foreach (var article in cart)
                {
                    OrderRowDB.InsertNewOrderRow(new OrderRow{
                        OrderRowID = article.OrderRowID,
                        Quantity = article.Quantity,
                       ArticleID = article.ArticleID,
                       OrderID = orderHead.OrderID});
                }

                Session["OrderHead"] = orderHead.OrderID;

                Session["OrderInformation"] = new OrderInformation(
                    totalSum: LabelTotal.Text,
                    currentUser: new User{
                        Username= currentUser.Username,
                        Firstname = TextBoxFirstName.Text,
                        Lastname = TextBoxLastName.Text,
                        Address = TextBoxAddress.Text,
                        Personnummer =  currentUser.Personnummer,
                        Zip = TextBoxZip.Text,
                        City = TextBoxCity.Text,
                        Email = TextBoxEmail.Text,
                        Phone = TextBoxPhone.Text,
                        UserID = currentUser.UserID
                        });

                cart.Clear();
                cartArticles.Clear();

                Response.Redirect("~/ConfirmOrder.aspx");
            }
            else
            {
                LabelNothingInCart.Visible = true;
            }
        }
    }
}